<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateShopOfflineUpdate extends Migrator
{
    /**
     * 商城 自动发货更新
     */
    public function up()
    {
        // 商品表
        $table = $this->table('shop_goods');
        $table->addColumn(Column::tinyInteger('is_offline')->setDefault(0)->setAfter('dispatch_id')->setComment('线下付款:0=否,1=是'))
            ->update();


        // 订单表
        $table = $this->table('shop_order');
        $table->changeColumn(Column::enum('status', ['closed', 'cancel', 'unpaid', 'paid', 'completed', 'pending'])->setDefault('unpaid')->setComment('订单状态:closed=交易关闭,cancel=已取消,unpaid=未支付,paid=已支付,completed=已完成,pending=待定'))
            ->addColumn(Column::enum('pay_mode', ['online', 'offline'])->setDefault('online')->setAfter('paid_time')->setComment('支付模式:online=线上支付,offline=线下支付'))    
            ->update();


        // 订单action表
        $table = $this->table('shop_order_action');
        $table->changeColumn(Column::enum('order_status', ['closed', 'cancel', 'unpaid', 'paid', 'completed', 'pending'])->setDefault('unpaid')->setComment('订单状态:closed=交易关闭,cancel=已取消,unpaid=未支付,paid=已支付,completed=已完成,pending=待定'))
            ->changeColumn(Column::tinyInteger('dispatch_status')->setDefault(0)->setComment('发货状态:-1=拒收,0=未发货,1=已发货,2=已收货'))
            ->update();

        // 订单售后表
        $table = $this->table('shop_order_aftersale');
        $table->changeColumn(Column::tinyInteger('dispatch_status')->setDefault(0)->setComment('发货状态:-1=拒收,0=未发货,1=已发货,2=已收货'))
            ->update();

        // 订单售后记录表
        $table = $this->table('shop_order_aftersale_log');
        $table->changeColumn(Column::tinyInteger('dispatch_status')->setDefault(0)->setComment('发货状态:-1=拒收,0=未发货,1=已发货,2=已收货'))
            ->update();


        // 订单item表
        $table = $this->table('shop_order_item');
        $table->changeColumn(Column::tinyInteger('dispatch_status')->setDefault(0)->setComment('发货状态:-1=拒收,0=未发货,1=已发货,2=已收货'))
            ->update();


        // pay表
        $table = $this->table('shop_pay');
        $table->changeColumn(Column::enum('pay_type', ['wechat', 'alipay', 'money', 'score', 'offline'])->setNull(true)->setDefault(null)->setComment('支付方式:wechat=微信支付,alipay=支付宝,money=钱包支付,score=积分支付,offline=线下支付'))
            ->update();

        // refund
        $table = $this->table('shop_refund');
        $table->changeColumn(Column::enum('pay_type', ['wechat', 'alipay', 'money', 'score', 'offline'])->setNull(true)->setDefault(null)->setComment('支付方式:wechat=微信支付,alipay=支付宝,money=钱包支付,score=积分支付,offline=线下支付'))
            ->update();

        
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        // 商品表
        $table = $this->table('shop_goods');
        $table->removeColumn('is_offline')
            ->update();

        // 订单表
        $table = $this->table('shop_order');
        $table->changeColumn(Column::enum('status', ['closed', 'cancel', 'unpaid', 'paid', 'completed'])->setDefault('unpaid')->setComment('订单状态:closed=交易关闭,cancel=已取消,unpaid=未支付,paid=已支付,completed=已完成'))
            ->removeColumn('pay_mode')    
            ->update();

        // 订单action表
        $table = $this->table('shop_order_action');
        $table->changeColumn(Column::enum('order_status', ['closed', 'cancel', 'unpaid', 'paid', 'completed'])->setDefault('unpaid')->setComment('订单状态:closed=交易关闭,cancel=已取消,unpaid=未支付,paid=已支付,completed=已完成'))
            ->changeColumn(Column::tinyInteger('dispatch_status')->setDefault(0)->setComment('发货状态:0=未发货,1=已发货,2=已收货'))
            ->update();

        // 订单售后表
        $table = $this->table('shop_order_aftersale');
        $table->changeColumn(Column::tinyInteger('dispatch_status')->setDefault(0)->setComment('发货状态:0=未发货,1=已发货,2=已收货'))
            ->update();

        // 订单售后记录表
        $table = $this->table('shop_order_aftersale_log');
        $table->changeColumn(Column::tinyInteger('dispatch_status')->setDefault(0)->setComment('发货状态:0=未发货,1=已发货,2=已收货'))
            ->update();

        // 订单item表
        $table = $this->table('shop_order_item');
        $table->changeColumn(Column::tinyInteger('dispatch_status')->setDefault(0)->setComment('发货状态:0=未发货,1=已发货,2=已收货'))
            ->update();

        // pay表
        $table = $this->table('shop_pay');
        $table->changeColumn(Column::enum('pay_type', ['wechat', 'alipay', 'money', 'score'])->setNull(true)->setDefault(null)->setComment('支付方式:wechat=微信支付,alipay=支付宝,money=钱包支付,score=积分支付'))
            ->update();

        // pay表
        $table = $this->table('shop_refund');
        $table->changeColumn(Column::enum('pay_type', ['wechat', 'alipay', 'money', 'score'])->setNull(true)->setDefault(null)->setComment('支付方式:wechat=微信支付,alipay=支付宝,money=钱包支付,score=积分支付'))
            ->update();
    }
}
